package com.dzxcqmxm.Dao.impl;

import com.dzxcqmxm.Dao.AlbumDao;
import com.dzxcqmxm.Dao.Dao;
import com.dzxcqmxm.JB.Album;
import com.dzxcqmxm.JB.User;

import javax.servlet.ServletContext;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Payboi
 * @create 2022/6/2-19:51
 * @FileName AlbumDaoImpl
 * @Description
 */
public class AlbumDaoImpl implements AlbumDao {
    /**
     * 因为相册是动态新增，所以比较麻烦，新增到数据库里还好，但是我要和新增到本地文件夹里边的结合起来
     * 以及这个是相册表，我新增得新增一整条语句，就比较麻烦
     *
     *
     *
     * @param album
     * @throws Exception
     */
    @Override
    public void addAlbum(Album album) throws Exception {
        //数据库插入数据
        String sql4 = "INSERT INTO `album` VALUES (?,?,?,?,?,?);";
        try {
            //连接数据源
            Connection connection = getConnection();
            //得到语句对象
            PreparedStatement pstm = connection.prepareStatement(sql4);
            //设置插入值
            pstm.setString(1,album.getXTXCName());
            pstm.setString(2,album.getXTXCUrl());
            pstm.setString(3,album.getUserName());
            pstm.setString(4,album.getAlbumName());
            pstm.setString(5,album.getAlbumUrl());
            pstm.setString(6,album.getAlbumBaoMi());
            //执行插入语句
            pstm.executeUpdate();

            //关闭连接，防止卡住！
            Dao.close(null,pstm,connection);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("插入数据失败！");
        }

    }

    /**
     * 这个是给首页的查找公开相册用的
     * 通过是否保密以及相册名来查找
     * 返回数组对象是因为
     * 同名的东西可能会有
     */
    @Override
    public ArrayList<Album> albumFindByNameAndAlbumBaoMi(String AlbumName,String AlbumBaoMi) throws Exception {

        String sql = "select * From album where AlbumName =? And AlbumBaoMi=?";
        ArrayList<Album> albumArrayList = new ArrayList<Album>();
        //连接数据源
        Connection connection = getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,AlbumName);
        pstm.setString(2,AlbumBaoMi);

        ResultSet rs = pstm.executeQuery();

        while (rs.next()){
            Album album = new Album();
            album.setXTXCName(rs.getString("XTXCName"));
            album.setXTXCUrl(rs.getString("XTXCUrl"));
            album.setUserName(rs.getString("UserName"));
            album.setAlbumName(rs.getString("AlbumName"));
            album.setAlbumUrl(rs.getString("AlbumUrl"));
            album.setAlbumBaoMi(rs.getString("AlbumBaoMi"));
            albumArrayList.add(album);
        }
        //关闭连接
        Dao.close(rs,pstm,connection);
        return albumArrayList;
    }

    @Override
    public ArrayList<Album> findAllAlbum() throws Exception {
        String sql4 = "select * from album";
        ArrayList<Album> albumArrayList = new ArrayList<Album>();
        try {
            Connection connection = getConnection();
            PreparedStatement preparedStatement2 = connection.prepareStatement(sql4);
            //执行查询语句（赋值给结果集）
            ResultSet rSt = preparedStatement2.executeQuery();
            while (rSt.next()){
                /**
                 * 这里我只想获得系统相册名，其他先
                 */
                Album album = new Album();
                album.setXTXCName(rSt.getString("XTXCName"));
                album.setXTXCUrl(rSt.getString("XTXCUrl"));
                album.setUserName(rSt.getString("UserName"));
                album.setAlbumName(rSt.getString("AlbumName"));
                album.setAlbumUrl(rSt.getString("AlbumUrl"));
                album.setAlbumBaoMi(rSt.getString("AlbumBaoMi"));

                albumArrayList.add(album);
            }
            //关闭连接，防止卡住！
            Dao.close(rSt,preparedStatement2,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询所有失败");
            return null;

        }
        return albumArrayList;
    }

    /**
     * 这里得设计一个多表删除
     * 也就是，相册和照片表里的数据一起删除掉
     *
     * @param AlbumName
     * @throws Exception
     */

    @Override
    public void delAlbum(String AlbumName) throws Exception {
        String sql = "DELETE album,photo from album LEFT JOIN photo ON album.AlbumName=photo.AlbumName WHERE album.AlbumName=?";

        try {
            Connection connection = getConnection();
            PreparedStatement pstm = connection.prepareStatement(sql);

            //设置要删除的语句，这个获取的id是就是上面那个where id啦
            pstm.setString(1,AlbumName);
            //执行了删除语句
            pstm.executeUpdate();
            //关闭连接，防止卡住！
            Dao.close(null,pstm,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败！！");

        }
    }

    /**
     * 打算更改的就是保密属性而已
     *
     */

    @Override
    public Album updateAlbumName(Album album) throws Exception {
        String sql2 = "update album set AlbumBaoMi=? where AlbumName=?";
        Connection connection = getConnection();

        try {
            PreparedStatement preparedStatement4 = connection.prepareStatement(sql2);//得到语句对象
            preparedStatement4.setString(1,album.getAlbumBaoMi());
            preparedStatement4.setString(2,album.getAlbumName());

            preparedStatement4.executeUpdate();
            //关闭连接，防止卡住！
            Dao.close(null,preparedStatement4,connection);

        } catch (SQLException e) {
            System.out.println("更新数据失败!!!!");
            e.printStackTrace();
        }

        return album;
    }

    /**
     * 通过系统相册名来展示里边对应的图片
     */

    @Override
    public ArrayList<Album> findAlbumByUserNameAndXTXCName(String UserName,String XTXCName) throws Exception {

        String sql = "select * From album where UserName =? And XTXCName=?";
        ArrayList<Album> albumArrayList = new ArrayList<Album>();
        //连接数据源
        Connection connection = getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,UserName);
        pstm.setString(2,XTXCName);

        ResultSet rs = pstm.executeQuery();

        while (rs.next()){
            Album album = new Album();
            album.setXTXCName(rs.getString("XTXCName"));
            album.setXTXCUrl(rs.getString("XTXCUrl"));
            album.setUserName(rs.getString("UserName"));
            album.setAlbumName(rs.getString("AlbumName"));
            album.setAlbumUrl(rs.getString("AlbumUrl"));
            album.setAlbumBaoMi(rs.getString("AlbumBaoMi"));
            albumArrayList.add(album);
        }
        //关闭连接
        Dao.close(rs,pstm,connection);
        return albumArrayList;
    }
}
